package mycoffee.order.activity;

import mycoffee.order.database.DatabaseConnection;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * 
 * @author Tuong
 * 
 */
public class MyCoffeeApplication extends Application {
	/** true: running on debug mode, otherwise, running on release mode. */
	private static boolean mIsDebugMode = false;
	public static int mLanguage = 0;
	/** use to connect with our application's database. */
	public static DatabaseConnection mDatabaseConnection;

	@Override
	public void onCreate() {
		// init global variable.
		mIsDebugMode = isDebuggable(getApplicationContext());
		super.onCreate();
	}

	/**
	 * check to know this app is running on debug mode or release mode.
	 * 
	 * @param context
	 * @return true: debug mode. false: release mode.
	 */
	private boolean isDebuggable(Context context) {

		boolean debuggable = false;
		PackageManager pm = context.getPackageManager();
		try {
			ApplicationInfo appinfo = pm.getApplicationInfo(
					context.getPackageName(), 0);
			debuggable = (0 != (appinfo.flags &= ApplicationInfo.FLAG_DEBUGGABLE));
		} catch (NameNotFoundException e) {

		}
		return debuggable;
	}

	public static boolean isDebugMode() {
		return mIsDebugMode;
	}

	/**
	 * Called when start app to initialize global objects that used for all
	 * activities.
	 * 
	 */
	public static void startApp(Context context) {
		// start database connection.
		if (mDatabaseConnection == null) {
			mDatabaseConnection = new DatabaseConnection(context);
			mDatabaseConnection.open();
		}
	}

	/**
	 * Close or release global objects when finish app.
	 */
	public static void finishApp() {
		if (mDatabaseConnection != null) {
			mDatabaseConnection.close();
			mDatabaseConnection = null;
		}
	}
}
